package br.com.erpcortume.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.com.erpcortume.beans.Contato;
import br.com.erpcortume.services.ContatoServices;

@Controller
@RequestMapping(value = "/contato")
public class ContatoController {
	
	private static final String CONTEXT_PAGE = "pages";
	
	@Autowired
	private ContatoServices contatoServices;

	@RequestMapping(value = "/novo")
	public String novo(){
		return CONTEXT_PAGE+"/contato/contato";
	}
	
	
	@RequestMapping(value = "/salvar", method = RequestMethod.POST)
	public String salvar(Contato contato){
		contatoServices.salvar(contato);
		return "redirect:listar";
	}
	
	
	@RequestMapping(value = "listar")
	public ModelAndView listar(){
		
		List<Contato> contatos = contatoServices.listar();
		
		ModelAndView modelAndView = new ModelAndView(CONTEXT_PAGE+"/contato/lista");
		modelAndView.addObject(contatos);
		
		return modelAndView;
	}
	
	
	@RequestMapping(value = "/toEdit/{id}", method = RequestMethod.GET)
	public ModelAndView toEdit(@PathVariable int id){
		
		Contato contato = contatoServices.findById(id);
		ModelAndView modelAndView = new ModelAndView(CONTEXT_PAGE+"/contato/contato");
		modelAndView.addObject(contato);
		
		return modelAndView;
	}
	
	
	@RequestMapping(value = "/excluir/{id}", method = RequestMethod.GET)
	public String excluir(@PathVariable int id){
		contatoServices.excluir(id);
		return "redirect:/contato/listar";
	}
	
}
